
//
//  selectAddView.m
//  DJS
//
//  Created by 浪尖渝力 on 2018/2/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "selectAddView.h"

@implementation selectAddView
-(void)creatView{
     self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UIView *view=[UIView new];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(_mainW*0.8);
        make.height.mas_equalTo(_mainH*0.6);
    }];
    view.layer.cornerRadius=5;
    view.backgroundColor=[UIColor whiteColor];
    
    UIView *topView=[UIView new];
    [view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    topView.backgroundColor=_backgroundColor;

    
    UILabel *titleLabel=[UILabel new];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    titleLabel.text=@"选择地区";
    titleLabel.font=[UIFont systemFontOfSize:18];
    titleLabel.textColor=[UIColor whiteColor];
    
    _tableView=[BaseTableView new];
    [view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView.mas_bottom);
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW*0.35);
    }];
    _tableView.tag=1;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
    
    _tableView2=[BaseTableView new];
    [view addSubview:_tableView2];
    [_tableView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topView.mas_bottom);
        make.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW*0.45);
    }];
    _tableView2.tag=2;
    _tableView2.delegate=self;
    _tableView2.dataSource=self;
    
    NSDictionary *dic1=_shengArr[0];
    NSNumber *gtype_one_id=[dic1 objectForKey:@"id"];
    NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
    NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"id", nil];
    [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic2 andTableView:_tableView2 andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        _shiArr=responseObject;
        [_tableView2 reloadData];
    } anderrorBlock:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==1) {
        return _shengArr.count;
    }
    return _shiArr.count;
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] init];

    if (tableView.tag==1) {
        NSDictionary *dic=_shengArr[indexPath.row];
        cell.backgroundColor=[UIColor whiteColor];
        cell.textLabel.text=[dic objectForKey:@"name"];
    }else {
        NSDictionary *dic=_shiArr[indexPath.row];
        cell.backgroundColor=_LightGrey;
        cell.textLabel.text=[dic objectForKey:@"name"];
    }
   
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font=[UIFont systemFontOfSize:18];
    cell.textLabel.textColor=ColorWithRGB(49, 49, 49);
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView.tag==1) {
        NSDictionary *dic1=_shengArr[indexPath.row];
        NSNumber *gtype_one_id=[dic1 objectForKey:@"id"];
        NSString *idt=[NSString stringWithFormat:@"%@",gtype_one_id];
        NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:idt,@"id", nil];
        
        [ZQTools AFNPostDataUrl:@"api/index/area_list" Dict:dic2 andTableView:_tableView2 andViewController:self.viewController andSuccessBlock:^(id responseObject) {
            _shiArr=responseObject;
            [_tableView2 reloadData];
        } anderrorBlock:nil];
    }
    else{
        [self removeFromSuperview];
        NSDictionary *dic1=_shiArr[indexPath.row];
        _customSelectedBlock(dic1);
    }
}

- (void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        cell.layoutMargins = UIEdgeInsetsZero;
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsZero;
    }
}

#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    
//    make.center.mas_equalTo(0);
//    make.width.mas_equalTo(_mainW*0.8);
//    make.height.mas_equalTo(_mainH*0.6);
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self ];
    
    if (!(touchPoint.y>_mainH/2-_mainH*0.3 && touchPoint.y<_mainH/2+_mainH*0.3 && touchPoint.x>_mainW*0.1 && touchPoint.x<_mainW*0.9)) {
        [self removeFromSuperview];
    }
}

@end
